Home | History | Annotate | Download | only in Tree
      1 /*
      2  * [The "BSD licence"]
      3  * Copyright (c) 2005-2008 Terence Parr
      4  * All rights reserved.
      5  *
      6  * Conversion to C#:
      7  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 namespace Antlr.Runtime.Tree
     34 {
     35     public interface ITreeNodeStream<T>
     36     {
     37         /** <summary>
     38          *  Get a tree node at an absolute index i; 0..n-1.
     39          *  If you don't want to buffer up nodes, then this method makes no
     40          *  sense for you.
     41          *  </summary>
     42          */
     43         T this[int i]
     44         {
     45             get;
     46         }
     47 
     48         /** <summary>
     49          *  Get tree node at current input pointer + i ahead where i=1 is next node.
     50          *  i&lt;0 indicates nodes in the past.  So LT(-1) is previous node, but
     51          *  implementations are not required to provide results for k &lt; -1.
     52          *  LT(0) is undefined.  For i&gt;=n, return null.
     53          *  Return null for LT(0) and any index that results in an absolute address
     54          *  that is negative.
     55          *  </summary>
     56          *
     57          *  <remarks>
     58          *  This is analogus to the LT() method of the TokenStream, but this
     59          *  returns a tree node instead of a token.  Makes code gen identical
     60          *  for both parser and tree grammars. :)
     61          *  </remarks>
     62          */
     63         T LT(int k);
     64 
     65         /** <summary>
     66          *  Where is this stream pulling nodes from?  This is not the name, but
     67          *  the object that provides node objects.
     68          *  </summary>
     69          */
     70         object TreeSource
     71         {
     72             get;
     73         }
     74 
     75         /** <summary>
     76          *  If the tree associated with this stream was created from a TokenStream,
     77          *  you can specify it here.  Used to do rule $text attribute in tree
     78          *  parser.  Optional unless you use tree parser rule text attribute
     79          *  or output=template and rewrite=true options.
     80          *  </summary>
     81          */
     82         ITokenStream TokenStream
     83         {
     84             get;
     85         }
     86 
     87         /** <summary>
     88          *  What adaptor can tell me how to interpret/navigate nodes and
     89          *  trees.  E.g., get text of a node.
     90          *  </summary>
     91          */
     92         ITreeAdaptor<T> TreeAdaptor
     93         {
     94             get;
     95         }
     96 
     97         /** <summary>
     98          *  As we flatten the tree, we use UP, DOWN nodes to represent
     99          *  the tree structure.  When debugging we need unique nodes
    100          *  so we have to instantiate new ones.  When doing normal tree
    101          *  parsing, it's slow and a waste of memory to create unique
    102          *  navigation nodes.  Default should be false;
    103          *  </summary>
    104          */
    105         bool UniqueNavigationNodes
    106         {
    107             get;
    108             set;
    109         }
    110 
    111         /** <summary>
    112          *  Return the text of all nodes from start to stop, inclusive.
    113          *  If the stream does not buffer all the nodes then it can still
    114          *  walk recursively from start until stop.  You can always return
    115          *  null or "" too, but users should not access $ruleLabel.text in
    116          *  an action of course in that case.
    117          *  </summary>
    118          */
    119         string ToString(T start, T stop);
    120 
    121 
    122         #region REWRITING TREES (used by tree parser)
    123 
    124         /** <summary>
    125          *  Replace from start to stop child index of parent with t, which might
    126          *  be a list.  Number of children may be different
    127          *  after this call.  The stream is notified because it is walking the
    128          *  tree and might need to know you are monkeying with the underlying
    129          *  tree.  Also, it might be able to modify the node stream to avoid
    130          *  restreaming for future phases.
    131          *  </summary>
    132          *
    133          *  <remarks>
    134          *  If parent is null, don't do anything; must be at root of overall tree.
    135          *  Can't replace whatever points to the parent externally.  Do nothing.
    136          *  </remarks>
    137          */
    138         void ReplaceChildren(T parent, int startChildIndex, int stopChildIndex, T t);
    139 
    140         #endregion
    141     }
    142 }
    143